package com.dinggou.shopjb.activitys;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.style.cityjd.JDCityConfig;
import com.lljjcoder.style.cityjd.JDCityPicker;
import com.dinggou.shopjb.R;
import com.dinggou.shopjb.base.BaseActivity;
import com.dinggou.shopjb.bean.AddressBean;
import com.dinggou.shopjb.net.ActionCallbackListener;
import com.dinggou.shopjb.utils.RouteUtil;
import com.dinggou.shopjb.utils.ToastUtil;
import com.dinggou.shopjb.view.MyEditText;

public class AddAddressActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_right, tv_city;
    private MyEditText met_name, met_mobile, met_code, met_address;
    private RelativeLayout rl_city;
    private CheckBox cb_default;
    private Button btn_submit;
    private AddressBean mAddressBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAddressBean = (AddressBean) getIntent().getSerializableExtra(RouteUtil.BUNDLE_ADDRESS);
        initData();
    }

    private void initData() {
        if (null != mAddressBean && !TextUtils.isEmpty(mAddressBean.id)) {
            String address = mAddressBean.address;

            met_name.setText(mAddressBean.name);
            met_mobile.setText(mAddressBean.mobile);
            tv_city.setText(mAddressBean.region);
            met_code.setText(mAddressBean.code);
            met_address.setText(address);
            cb_default.setChecked(mAddressBean.is_default == 1 ? true : false);
            tv_right.setVisibility(View.VISIBLE);
        } else {
            tv_right.setVisibility(View.GONE);
        }
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void findViews() {
        tv_right = findViewById(R.id.tv_right);
        met_name = findViewById(R.id.met_name);
        met_mobile = findViewById(R.id.met_mobile);
        rl_city = findViewById(R.id.rl_city);
        tv_city = findViewById(R.id.tv_city);
        met_code = findViewById(R.id.met_code);
        met_address = findViewById(R.id.met_address);
        cb_default = findViewById(R.id.cb_default);
        btn_submit = findViewById(R.id.btn_submit);
    }

    @Override
    protected void setListeners() {
        tv_right.setOnClickListener(this);
        rl_city.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == tv_right) {
            delete();
        } else if (v == rl_city) {
            showAddressPickerPop();
        } else if (v == btn_submit) {
            createAddress();
        }
    }

    /**
     * 创建地址
     */
    public void createAddress() {
        String name = met_name.getText();
        String mobile = met_mobile.getText();
        String code = met_code.getText();
        String address = met_address.getText();
        String region = tv_city.getText().toString();
        int defaultId = cb_default.isChecked() ? 1 : 0;

        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(mobile)
                || TextUtils.isEmpty(address)) {
            ToastUtil.showToast(mContext, "请确保信息填写完毕");
            return;
        }
        if (null == mAddressBean) {
            mAddressBean = new AddressBean();
        }
        mAddressBean.name = name;
        mAddressBean.mobile = mobile;
        mAddressBean.code = code;
        mAddressBean.address = address;
        mAddressBean.region = region;
        mAddressBean.is_default = defaultId;

        loading();
        if (TextUtils.isEmpty(mAddressBean.id)) {
            mAppAction.createAddress(mAddressBean, new ActionCallbackListener<Void>() {
                @Override
                public void onSuccess(Void data) {
                    cancelLoading();
                    ToastUtil.showToast(mContext, "添加成功");
                    backActivity();
                }

                @Override
                public void onFailure(String errorCode, String message) {
                    ToastUtil.showToast(mContext, message);
                    cancelLoading();
                }
            });
        } else {
            mAppAction.editAddress(mAddressBean, new ActionCallbackListener<Void>() {
                @Override
                public void onSuccess(Void data) {
                    cancelLoading();
                    ToastUtil.showToast(mContext, "修改成功");
                    backActivity();
                }

                @Override
                public void onFailure(String errorCode, String message) {
                    ToastUtil.showToast(mContext, message);
                    cancelLoading();
                }
            });
        }

    }

    /**
     * 地区选择弹窗
     */
    private void showAddressPickerPop() {
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(tv_city.getWindowToken(), 0);

        JDCityPicker cityPicker = new JDCityPicker();
        JDCityConfig jdCityConfig = new JDCityConfig.Builder().build();

        jdCityConfig.setShowType(JDCityConfig.ShowType.PRO_CITY_DIS);
        cityPicker.init(this);
        cityPicker.setConfig(jdCityConfig);
        cityPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                String address = province.getName();
                met_code.setText(city.getId());
                tv_city.setText(address);
                met_address.setText(address + city.getName() + district.getName());
            }

            @Override
            public void onCancel() {
            }
        });
        cityPicker.showCityPicker();
    }

    private void delete() {
        loading();
        mAppAction.deleteAddress(mAddressBean.id, new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                cancelLoading();
                ToastUtil.showToast(mContext, "删除成功");
                backActivity();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext, message);
                cancelLoading();
            }
        });
    }
}